<?php
	/**
	* 封装一个验证码类
	*/
		Class Auto_code{
			private $width; //验证码的宽度
			private $height;//验证码的高度
			private $font_nums;//验证码的数量
			// private $img_color; //画布的背景颜色
			private $font_color;//验证码的颜色
			private $font_size;//验证码的大小
			// public static $img_size;
			public $img_size;
			public $mun;

		//  function __construct($width,$height,$font_nums=null){
		//  	$this->width = $width;
		//  	$this->height = $height;
		//  	$this->font_nums = $font_nums;
		// }
		function __construct($config=array()){
			$this->width = isset($config['width']) ? $config['width'] : C('DB_WIDTH');
			$this->height = isset($config['height']) ? $config['height'] : C('DB_HEIGHT');
			$this->font_nums = isset($config['font_nums']) ? $config['font_nums'] : C('FONT_NUMS');
			// $this->$font_color = isset($config['name']) ? $config['name'] : C('UPLOAD_NAME');
		}

		//设置画布的大小及配置颜色
		function setImgWidthHeight(){
			// 发送头部信息
			// header('Content-type: image/png');
			$this->img_size = imagecreatetruecolor($this->width,$this->height);
			$this->img_color = imagecolorallocate($this->img_size, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
			imagefill($this->img_size, 0, 0, $this->img_color);
			$this->show_char();
			$this->disturb();
			imagepng($this->img_size); //将画布以PNG的形式输出。
			imageDestroy($this->img_size); //最后摧毁画布。
			return $this->mun;
		}

		//产生四个验证码
		 private function show_char(){
		    $str = "qwertyuiopasdfghjklzxcvbnm1234567890"; //设置一些产生的字符串。
		    $width = 15;//设置宽的初始值为40;
		    for ($i=0; $i < $this->font_nums; $i++) { 
		    	$str_one = $str[mt_rand(0,35)];
		    	$img_color = imagecolorallocate($this->img_size, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//设置画布的颜色
		    	ImageTTFText($this->img_size, 30, mt_rand(-45,45), $width, 35, $img_color, "./Jathilan-Regular.ttf", $str_one);
		    	$width += 35;
		    	$this->mun.=$str_one;
		    }	    
		}
		//产生验证码的干扰线
		private function disturb(){
			// echo "hello";
			$height = 0;
			for ($i=0; $i < 5; $i++) { 
				$height += 10;
				imageline ( $this->img_size, 20, $height, 200, $height, mt_rand(0,255));
			}
		}


		}

?>